<?php

/*

 * @package		Joomla.Framework

 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.

 * @license		GNU General Public License version 2 or later; see LICENSE.txt

 *

 * @component Phoca Component

 * @copyright Copyright (C) Jan Pavelka www.phoca.cz

 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License version 2 or later;

 */

defined('_JEXEC') or die();



class JFormFieldRegionList extends JFormField

{

	protected $type 		= 'RegionList';



	protected function getInput() {

		$userId	= (string) $this->form->getValue($this->element['name']);	

		$db = & JFactory::getDBO();
		
		$tid = $_REQUEST['id'];
		if(!$tid)
			$db->setQuery("select id,title , parent as parent_id from #__regions ORDER BY parent, ordering ");
		else
			$db->setQuery("select id,title , parent as parent_id from #__regions where id!=$tid ORDER BY parent, ordering ");
		

		$rows = $db->loadObjectList();
		
		$levellimit = 10;
		
		$children = array();
		// first pass - collect children
		foreach ($rows as $v )
		{
			$pt = $v->parent_id;
			$list = @$children[$pt] ? $children[$pt] : array();
			array_push( $list, $v );
			$children[$pt] = $list;
		}


		// second pass - get an indent list of the items
		$list = JHTML::_('menu.treerecurse', 0, '', array(), $children, max( 0, $levellimit-1 ) ,0,0);
		$first[] = array('id'=>0,'treename'=>'Root');

		$final = array_merge($first,$list);

		return JHTML::_("select.genericlist",$final,'jform['.$this->element['name'].']',$this->element['class'],'id','treename',$this->value);

	}

}

?>